package com.scirillo.frases.notification;
import com.scirillo.frases.R;
import com.scirillo.frases.ui.ViewPagerFragmentActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Vibrator;


public class FrasesNotificationManager {

	
	private static Context mAContext;
	private static NotificationManager mNotificationManager;
	private static MediaPlayer mp;

	public static void init(Context argContext)
	{		
		mAContext = argContext;
		mNotificationManager = (NotificationManager) argContext.getSystemService(Context.NOTIFICATION_SERVICE);

	}
	
	@SuppressWarnings("deprecation")
	public static void displaytNotification()
	{
	//	if(!(Util.getInstance().getCurrentContext() instanceof ViewPagerFragmentActivity)){
			Intent intent = new Intent(mAContext, ViewPagerFragmentActivity.class);
			PendingIntent contentIntent = PendingIntent.getActivity(mAContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			Notification notification = new Notification(android.R.drawable.ic_notification_overlay,mAContext.getResources().getString(R.string.notification), System.currentTimeMillis());
			notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
			NotificationManager manager = (NotificationManager) mAContext.getSystemService(Context.NOTIFICATION_SERVICE);	
			notification.setLatestEventInfo(mAContext,mAContext.getResources().getString(R.string.notification),"", contentIntent);
			manager.notify(1, notification);	
			alertForIm();	
//		}
	}
	
	public static void clearNotifications()
	{
		
		if (mNotificationManager != null)
		{
			mNotificationManager.cancel(1);
		}
	}

	
	public static void alertForIm()
	{
		AudioManager audio = (AudioManager) mAContext.getSystemService(Context.AUDIO_SERVICE);
		if (audio.getRingerMode() != AudioManager.RINGER_MODE_SILENT)
		{
			((Vibrator) mAContext.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(500);
		}
		if (audio.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
		{
			try
			{		
				Uri p = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);				
				mp = MediaPlayer.create(mAContext, p);
				CountDownTimer cntr_aCounter = new CountDownTimer(3000, 1000) 
				{
			        public void onTick(long millisUntilFinished) 
			        {
			        	try
			        	{
			        		mp.start();
			        	}				
			        	catch (Exception e) {}
			        }
			        public void onFinish() 
			        {
			        	try
			        	{
			        		mp.stop();
			        	}				
			        	catch (Exception e) {}
			        }
			    };
			    cntr_aCounter.start();											
			}
			catch (Exception e)
			{
					
			}
		}
	}
}
